package com.jackrain.nea.core.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.jackrain.nea.core.api.AdRefbyTableDelCmd;
import com.jackrain.nea.core.mapper.AdTableMapper;
import com.jackrain.nea.core.mapper.MapperShortcut;
import com.jackrain.nea.core.utility.CommandData;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.sys.CommandAdapter;
import com.jackrain.nea.util.ValueHolder;
import com.jackrain.nea.web.query.QuerySession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;

@Slf4j
@Component
@Service(protocol = "dubbo", validation = "true", version = "1.0", group = "ad")
public class AdRefbyTableDelCmdImpl extends CommandAdapter implements AdRefbyTableDelCmd {

    @Autowired 
    private MapperShortcut mapperShortcut;


    @Autowired
    private AdTableMapper adTableMapper;
    @Override
    @Transactional
    public ValueHolder execute(QuerySession session) throws NDSException {
        CommandData data = new CommandData(session);
        
        long id = data.getObjId();

        if (data.isDeleting()) {
            if (!data.isDeleteMainTable()) {
                long[] ids = data.getItemsDeleteIds("AD_REFBYTABLE");
                if (ids != null && ids.length > 0) {
                    mapperShortcut.delete(ids, "AD_REFBYTABLE");
                }
            }
        }
        
        adTableMapper.updateModifieddate(new Timestamp(System.currentTimeMillis()),id);


        return data.success();
    }

}
